<?php

namespace app\stationpc\controller;

use app\model\StationMemberInfo as ModelStationMemberInfo;
use app\model\StationInfo as ModelStationInfo;
use app\model\StationOrderLog as ModelStationOrderLog;


class UserList extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 人员列表 \n
     * URI : /stationpc/member.info.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  mobile   | string | 选填    | 联系电话
     *  memberNumber      | string | 选填 | 员工编号
     *  memberName        | string | 选填 | 员工姓名
     *  order    | string | 选填    | 默认正序  （添加时间 order = 'desc'倒序）
     *
     * @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 列表
     *
     * data:
     *  name        | type   | description
     * -------------|--------|-------------
     *  memberId    | int    | 员工 (ID)
     *  mobile      | string | 联系电话
     *  memberName  | string | 员工姓名
     *  memberNumber         | string | 员工编号
     *  isUse       | int    | 是否冻结 0否 1是
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function memberInfoList()
    {
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);

        $str_mobile = $this->_getContentParams('mobile'); // 用户帐号（手机号）
        $str_member_number = $this->_getContentParams('memberNumber'); //
        $str_member_name = $this->_getContentParams('memberName');
        $str_order = $this->_getContentParams('order');

        $str_order = $this->listOrder('gmt_create', $str_order);

        $arr_where = $this->memberInfoListWhere($str_mobile, $str_member_number, $str_member_name);

        $int_total_rows = ModelStationMemberInfo::getInstance()->toCount($arr_where);

        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = 'id AS memberId,member_number AS memberNumber,member_name AS memberName,mobile,is_use AS isUse';
        $arr_list = ModelStationMemberInfo::getInstance()->toSelect($arr_where, $str_field, $int_first_row, $int_list_rows, $str_order);
        _returnData(['totalPages' => $int_total_pages, 'list' => $arr_list]);
    }

    /**
     * 人员列表 -> 判断筛选
     * @param $str_mobile
     * @param $str_member_number
     * @param $str_member_name
     * @return array
     */
    private function memberInfoListWhere($str_mobile, $str_member_number, $str_member_name)
    {
        $arr_where = [];

        if ($str_mobile != '') {
            $arr_where['mobile'] = ['like', "%$str_mobile%"];
        }
        if ($str_member_number !== '') {
            $arr_where['member_number'] = ['like', "%$str_member_number%"];
        }
        if ($str_member_name !== '') {
            $arr_where['member_name'] = ['like', "%$str_member_name%"];
        }
        return $arr_where;
    }

    /**
     * 人员列表 - 冻结（恢复）用户 \n
     * URI : /stationpc/is.freeze
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  memberId | int    | 必填 | 员工编号 (ID)
     *  isUse    | int    | 必填 | 是否冻结 0否 1是
     *
     * @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  ----------  | ------ | 无
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function isFreeze()
    {
        $int_member_id = $this->_getContentParams('memberId');
        _toValidate('id', $int_member_id, 10009);
        $int_is_use = $this->_getContentParams('isUse');
        _toCompare(in_array($int_is_use, [0, 1]), '===', false, 30004);
        ModelStationMemberInfo::getInstance()->toUpdate(['id' => $int_member_id], ['is_use' => $int_is_use]);
        _returnData();
    }

    /**
     * 人员列表->修改详情 \n
     * URI : /stationpc/member.list.modify.details
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  memberId | int    | 必填 | 员工编号
     *
     * @return :
     *  name            | type   | description
     * -----------------|--------|-------------
     *  memberId        | int    | 员工编号 (ID)
     *  mobile          | string | 电话
     *  memberName      | string | 姓名
     *  memberAddress   | string | 联系地址
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function memberListModifyDetails()
    {
        $int_user_id = $this->_getContentParams('memberId');
        _toValidate('id', $int_user_id, 10009);
        $arr_data = ModelStationMemberInfo::getInstance()->toFind(['id' => $int_user_id], 'id AS memberId,mobile,member_name AS memberName,member_address AS memberAddress');
        _returnData($arr_data);
    }

    /**
     * 人员列表->修改 \n
     * URI : /stationpc/member.list.modify
     * @param :
     *  name     | type | null| description
     * ----------|------|-----|-------------
     *  memberId        | int    | 必填 | 员工编号 (ID)
     *  mobile          | string | 必填 | 电话
     *  memberName      | string | 必填 | 姓名
     *  memberAddress   | string | 必填 | 联系地址
     *
     * @return :
     *  name     | type   | description
     * ----------|--------|-------------
     *  -------- | ------ | 无
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function memberListModify()
    {
        $int_member_id = $this->_getContentParams('memberId');
        _toValidate('id', $int_member_id, 10009);
        $str_mobile = $this->_getContentParams('mobile');
        _toValidate('mobile', $str_mobile, 12055);
        $str_member_name = $this->_getContentParams('memberName');
        _toCompare(empty($str_member_name), '===', true, 12003);
        $str_member_address = $this->_getContentParams('memberAddress');
        _toCompare(empty($str_member_address), '===', true, 30005);

        $arr_data = [
            'mobile' => $str_mobile,
            'member_name' => $str_member_name,
            'member_address' => $str_member_address,
        ];
        ModelStationMemberInfo::getInstance()->toUpdate(['id' => $int_member_id], $arr_data);
        _returnData();
    }

    /**
     * 人员列表 - 添加 \n
     * URI : /stationpc/member.list.add
     * @param :
     *  name     | type | null| description
     * ----------|------|-----|-------------
     *  mobile          | string | 必填 | 电话
     *  memberName      | string | 必填 | 姓名
     *  memberAddress   | string | 必填 | 联系地址
     *
     * @return :
     *  name     | type   | description
     * ----------|--------|-------------
     *  -------- | ------ | 默认密码为123456修改须用户设置中
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function memberListAdd()
    {
        $str_mobile = $this->_getContentParams('mobile');
        _toValidate('mobile', $str_mobile, 12055);
        $str_member_name = $this->_getContentParams('memberName');
        _toCompare(empty($str_member_name), '===', true, 12003);
        $str_member_address = $this->_getContentParams('memberAddress');
        _toCompare(empty($str_member_address), '===', true, 30005);
        $str_pwd_key = $this->_getRandomString(8);
        $arr_data = [
            'mobile' => $str_mobile,
            'member_name' => $str_member_name,
            'member_address' => $str_member_address,
            'pwd_key' => $str_pwd_key,
            'station_id' => $this->station_id,
            'password' => passwordEncryption('123456', $str_pwd_key),
        ];
        $int_info_id = ModelStationMemberInfo::getInstance()->toAdd($arr_data);
        ModelStationMemberInfo::getInstance()->toUpdate(['id' => $int_info_id], ['member_number' => getCharNumber($int_info_id, 'YG')]);
        _returnData();
    }


    /**
     * 人员列表 -> 查看 \n
     * URI : /stationpc/member.list.details
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  memberId    | int    | 必填 | 人员ID
     *  orderMember | int    | 选填 | 订单编号
     *  state       | int    | 选填 | '40：已入库；50封箱；55:拆箱；60：出库；65：中转；'
     *  gmtStart    | string | 选填 | 操作时间(开始:'2018-04-09 00:00:00')
     *  gmtEnd      | string | 选填 | 操作时间(结束:'2018-04-10 23:59:59')
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 人员详情
     *  list        | array  | 操作记录列表
     *
     * data:
     *  name        | type   | description
     * -------------|--------|-------------
     *  memberNumber| string | 员工编号
     *  memberName  | string | 订单号
     *  mobile      | float  | 订单金额
     *  unitName    | float  | 订单金额
     *
     * list:
     *  name        | type   | description
     * -------------|--------|-------------
     *  orderLogId  | string | 记录ID
     *  orderNumber | string | 订单编号
     *  gmtCreate   | string | 操作时间
     *  state       | int    | 操作状态'40：已入库；50封箱；55:拆箱；60：出库；65：中转；'
     *
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function memberListDetails()
    {
        $int_member_id = $this->_getContentParams('memberId');
        _toValidate('id', $int_member_id, 10009);
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);
        $int_order_member = $this->_getContentParams('orderMember');     //订单编号
        $int_state = $this->_getContentParams('state');     //订单状态
        $str_gmt_start = $this->_getContentParams('gmtStart'); // 时间（开始）
        $str_gmt_end = $this->_getContentParams('gmtEnd'); // 时间（结束）
        $arr_where = $this->memberListDetailsWhere($int_order_member, $int_state, $str_gmt_start, $str_gmt_end);

        $int_total_rows = ModelStationOrderLog::getInstance()->toCount($arr_where);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = 'id AS orderLogId,order_number AS orderNumber,state,gmt_create AS gmtCreate';
        $arr_list = ModelStationOrderLog::getInstance()->toSelect($arr_where, $str_field, $int_first_row, $int_list_rows, '');

        $arr_data = ModelStationMemberInfo::getInstance()->toSelect(['id' => $int_member_id], 'id AS memberId,member_number AS memberNumber,member_name AS memberName,mobile,station_id AS stationId');
        $arr_data = $this->userListDetailsModify($arr_data);
        _returnData(['data' => $arr_data, 'totalPages' => $int_total_pages, 'list' => $arr_list]);
    }

    /**
     * 人员列表 -> 筛选
     * @param $int_order_member
     * @param $int_state
     * @param $str_gmt_start
     * @param $str_gmt_end
     * @return array
     */
    public function memberListDetailsWhere($int_order_member, $int_state, $str_gmt_start, $str_gmt_end){
        $arr_where = [];
        if($int_order_member != ''){
            $arr_where['order_number'] = ['like', "%$int_order_member%"];
        }
        if($int_state != ''){
            $arr_where['state'] = $int_state;
        }
        if ($str_gmt_start != '' && $str_gmt_end != '') {
            _toCompare($str_gmt_start > $str_gmt_end, '===', true, 30003);
            $arr_where['gmt_create'] = ['between', [$str_gmt_start, $str_gmt_end]];
        }
        return $arr_where;
    }

    /**
     * 人员列表 -> 站点名称
     * @param $arr_data
     * @return mixed
     */
    public function userListDetailsModify($arr_data){
        foreach ($arr_data as &$value) {
            $value['unitName'] = ModelStationInfo::getInstance()->toValue(['id' => $value['stationId']], 'name');
        }
        return $arr_data;
    }
}
